
<?php

/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

//démarrer une session
session_start();

// Montrer les erreurs
ini_set('display_errors',1);
ini_set('display_startup_errors',1);
error_reporting(-1);

require_once( '../libs/facebook/src_v4/Facebook/FacebookSession.php' );
require_once( '../libs/facebook/src_v4/Facebook/FacebookRedirectLoginHelper.php' );
require_once( '../libs/facebook/src_v4/Facebook/FacebookRequest.php' );
require_once( '../libs/facebook/src_v4/Facebook/FacebookResponse.php' );
require_once( '../libs/facebook/src_v4/Facebook/FacebookSDKException.php' );
require_once( '../libs/facebook/src_v4/Facebook/FacebookRequestException.php' );
require_once( '../libs/facebook/src_v4/Facebook/FacebookAuthorizationException.php' );
require_once( '../libs/facebook/src_v4/Facebook/GraphObject.php' );
require_once( '../libs/facebook/src_v4/Facebook/GraphUser.php' );               
require_once( '../libs/facebook/src_v4/Facebook/Entities/AccessToken.php' );
require_once( '../libs/facebook/src_v4/Facebook/Entities/SignedRequest.php' );
require_once( '../libs/facebook/src_v4/Facebook/HttpClients/FacebookCurl.php' );
require_once( '../libs/facebook/src_v4/Facebook/HttpClients/FacebookHttpable.php' );
require_once( '../libs/facebook/src_v4/Facebook/HttpClients/FacebookCurlHttpClient.php' );
require_once( '../libs/facebook/src_v4/Facebook/HttpClients/FacebookGuzzleHttpClient.php' );                
require_once( '../libs/facebook/src_v4/Facebook/HttpClients/FacebookStream.php' );
require_once( '../libs/facebook/src_v4/Facebook/HttpClients/FacebookStreamHttpClient.php' );
         
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\GraphUser;

// includes
include_once '../model/config.php';
include_once 'UtilisateurManager.php';

//variables 
$page_accueil = "../view/accueil.php";
$user = "bill";

FacebookSession::setDefaultApplication('654946567937255','26051c2da3466847bf5615c2fd3952dc');
$helper = new FacebookRedirectLoginHelper('http://www.fixnfax.com/gestiontache/'.$user.'/controller/facebook.php');
                            
try {
   $session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
  // When Facebook returns an error
} catch(\Exception $ex) {
  // When validation fails or other local issues
}
if ($session) {
       // session facebook
       $request = new FacebookRequest($session, 'GET', '/me');
       $response = $request->execute();
       $graphObject = $response->getGraphObject();

       $user = $response->getGraphObject(GraphUser::className());
       $manager = new UtilisateurManager($bdd);
       $result = $manager->createUserFb($user);
       if ( $result  == 0) {
           $_SESSION["id"] = $bdd->lastInsertId();
       }else{
           $_SESSION["id"] = $result;
       }
       //mise en session
       $_SESSION["fid"] = $user->getId();
       $_SESSION['prenom'] = $user->getLastName();
       header("location:".$page_accueil);

}else{
      header("location:".$helper->getLoginUrl());     
}




         
            